<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/13 0013
 * Time: 17:26
 */

namespace app\admin\controller;
use think\Db;

class Zmzx  extends Bases
{
   public function index(){
       $where['is_del']=0;
       $where['sign']="pc";
       $data = Db::name('zmzx')->where($where)->order('sort desc,id desc')->select();

       $this->assign('data',$data);
       return $this->fetch();
   }


    public function edit_qqlp(){

        if($id){
            $data =Db::name('zmzx')->where('id',$id)->find();

            $this->assign('data',$data);

        }
        return $this->fetch();
    }

   public function add_zmzx(){
       $id = input('param.id');
        if(request()->isAjax()){
            $id = input('post.id');
            $data=[
                'title'=>input('post.title'),
                'is_show'=>intval(input('post.is_show')),
                'type'=>intval(input('post.type')),
                'sort'=>intval(input('post.sort')),
                'content'=>input('post.content'),
                'click'=>intval(input('post.click')),
                'seo_title'=>input('param.seo_title'),
                'seo_keywords'=>input('param.seo_keywords'),
                'seo_description'=>input('param.seo_description'),
                'sign'=>"pc"
            ];
            $file_main_img = request()->file('main_img');
            if($file_main_img){
                $file_info_main_img = $file_main_img->move('./uploads/zmzx/');
                if($file_info_main_img){
                    $data['main_img'] = '/uploads/zmzx/'.$file_info_main_img->getSaveName();
                }else{
                    return json(['code'=>0,'msg'=>$file_main_img->getError()]);
                }
            }
            if($id){
                if($file_main_img){
                    $old_img = Db::name('zmzx')->where('id',$id)->value('main_img');
                    if($old_img){
                        if($old_img !== ' '){
                            unlink('.'.str_replace('\\','/',$old_img));
                        }
                    }
                }
                $data['update_time']=time();
                $result = DB::name('zmzx')->where('id',$id)->update($data);
            }else{
                $data['create_time']=time();
                $result = Db::name('zmzx')->insert($data);
            }
            if($result !==false){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
       if($id){
           $data =Db::name('zmzx')->where('id',$id)->find();

           $this->assign('data',$data);

       }
       return $this->fetch();

   }

    //删除
    public function zmzxdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('zmzx')->where('id',$id)->delete();;
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //改变排序
    public function ChangeSort(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('zmzx')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }

    //热门新品
    public function hotnew(){
        $where['is_del']=0;
        $where['sign']="pc";
        $data = Db::name('hotnew')->where($where)->order('sort desc,id desc')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    //热门新品
    public function add_hotnew(){
        $id = input('param.id');
        if(request()->isAjax()){
            $id = input('post.id');
            $data=[
                'title'=>input('post.title'),
                'sub_title'=>input('post.sub_title'),
                'out_link'=>input('post.out_link'),
                'is_show'=>intval(input('post.is_show')),
                'sort'=>intval(input('post.sort')),
                'content'=>input('post.content'),
                'seo_title'=>input('param.seo_title'),
                'seo_keywords'=>input('param.seo_keywords'),
                'seo_description'=>input('param.seo_description'),
                'sign'=>"pc"
            ];
            $file_main_img = request()->file('main_img');
            if($file_main_img){
                $file_info_main_img = $file_main_img->move('./uploads/hotnew/');
                if($file_info_main_img){
                    $data['main_img'] = '/uploads/hotnew/'.$file_info_main_img->getSaveName();
                }else{
                    return json(['code'=>0,'msg'=>$file_main_img->getError()]);
                }
            }
            $file_vice_img = request()->file('vice_img');
            if($file_vice_img){
                $file_info_vice_img = $file_vice_img->move('./uploads/hotnew/');
                if($file_info_vice_img){
                    $data['vice_img'] = '/uploads/hotnew/'.$file_info_vice_img->getSaveName();
                }else{
                    return json(['code'=>0,'msg'=>$file_vice_img->getError()]);
                }
            }
            if($id){
                if($file_main_img){
                    $old_img = Db::name('hotnew')->where('id',$id)->value('main_img');
                    if($old_img !==' '){
                        unlink('.'.str_replace('\\','/',$old_img));
                    }
                }

                if($file_vice_img){
                    $old_img = Db::name('hotnew')->where('id',$id)->value('vice_img');
                    if($old_img){
                        unlink('.'.str_replace('\\','/',$old_img));
                    }
                }
                $data['update_time']=time();
                $result = DB::name('hotnew')->where('id',$id)->update($data);
            }else{
                $data['create_time']=time();
                $result = Db::name('hotnew')->insert($data);
            }
            if($result !==false){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
        if($id){
            $data =Db::name('hotnew')->where('id',$id)->find();

            $this->assign('data',$data);

        }
        return $this->fetch();
    }


    //删除
    public function hotnewdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('hotnew')->where('id',$id)->update(['is_del'=>1]);;
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //改变排序
    public function ChangeSorthotnew(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('hotnew')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }
}